import Vue from 'vue';
import zh_CN from 'vee-validate/dist/locale/zh_CN'; // 引入中文文件
import VeeValidate, { Validator,extend } from 'vee-validate'
import VueI18n from 'vue-i18n';
Vue.use(VueI18n)

const i18n = new VueI18n({
  locale: 'zh_CN'
})

const config = {
  errorBagName: 'errors', // change if property conflicts.
  fieldsBagName: 'fieldBags',  // 报冲突时 可自定义修改字段名称
  delay: 0, // 错误提示的延迟时间
  strict: true, // 没有设置规则的表单不进行校验，
  enableAutoClasses: false,
  locale: 'zh_CN', // 对语言（中文）的配置
  classNames: {
    touched: 'touched', // the control has been blurred
    untouched: 'untouched', // the control hasn't been blurred
    valid: 'valid', // model is valid
    invalid: 'invalid', // model is invalid
    pristine: 'pristine', // control has not been interacted with
    dirty: 'dirty' // control has been interacted with
  },
  events: 'input', //* *input|blur** 在用户输入和表单失去焦点时都进行校验 可单独写  blur或input
  inject: true,
  i18n,
  i18nRootKey: 'validation',
  dictionary: {
    zh_CN
  }
}
Vue.use(VeeValidate, config)


/**
 * 自定义dictionary
 */
const dictionary = {
  zh_CN: {
    messages: {
      required: (field) => field + '不能为空'
    },
    attributes: {
    	手机号码: '手机号码',
    },
    custom: {
    	// 需与组件的name属性值保持一致
    	密码: {
	      min: "太短了，6-8位",
	      password:"字母、数字和符号至少包含2种",
	    },
    	确认密码: {
	      min: "太短了，6-8位",
	      password:"字母、数字和符号至少包含2种",
	    },
	  },
  }
}
Validator.localize(dictionary);


/**
 * 自定义验证规则
 */
// 3.邮箱
Validator.extend('email', {
    messages: {
        zh_CN: field => '请输入正确格式的邮箱'
    },
    validate: value => {
        return /^[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?$/.test(value)
    }
})

// 4.身份证号
Validator.extend('idcard', {
    messages: {
        zh_CN: field => '请输入正确格式的身份证号'
    },
    validate: value => {
        return /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(value)
    }
})


// 5.手机号
Validator.extend('phone', {
    messages: {
        zh_CN: field => '请输入正确格式的手机号'
    },
    validate: value => {
        return /^1[2-9]\d{9}$/.test(value)
    }
})

// 6. 密码
Validator.extend('password', {
    validate: value => {
    		if(value.length >= 6){
    			return /(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^.{6,10}$/.test(value)
    		}
        return false
    }
})